﻿#include <QDesktopServices>
#include "CVideoResultQuery.h"
#include "CGlobal.h"
#include "CSqlOper.h"
#include "CGlobal.h"
#include "Public.h"

CVideoResultQuery::CVideoResultQuery(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("报告记录查询"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	m_nPerPageCount = 100;//默认每页显示100条数据
	m_nCurrentPage = 1;//当前页数第一页
	m_nTotalCount = 0;//记录总数
	m_nPageCount = 0;//总页数
	setStyleSheetFile();
	initConnect();
	initData();
	slotPushButtonQuery();
}

CVideoResultQuery::~CVideoResultQuery()
{}

void CVideoResultQuery::initConnect()
{
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CVideoResultQuery::slotPushButtonQuery);
	connect(ui.pushButtonDelDay, &QPushButton::clicked, this, &CVideoResultQuery::slotDelDay);
	connect(ui.pushButtonAddDay, &QPushButton::clicked, this, &CVideoResultQuery::slotAddDay);
	connect(ui.pushButtonUp, &QPushButton::clicked, this, &CVideoResultQuery::slotUp);
	connect(ui.pushButtonDown, &QPushButton::clicked, this, &CVideoResultQuery::slotDown);
	connect(ui.widget, &CTableWidgetB::sigShowResult, this, &CVideoResultQuery::slotOpenResult);
}

void CVideoResultQuery::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CVideoResultQuery::initData()
{
	//设置两时间编辑栏
	QDateTime timeTo = QDateTime::currentDateTime();
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = QDateTime::currentDateTime();
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	ui.dateTimeEditBegin->setCalendarPopup(true);
	ui.dateTimeEditEnd->setCalendarPopup(true);
	ui.widget->InitUi(3);
}

void CVideoResultQuery::slotPushButtonQuery()
{
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	QString sStartTime = ui.dateTimeEditBegin->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sEndTime = ui.dateTimeEditEnd->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	CSqlOper::instance().selectResult(sStartTime, sEndTime, vecRecord, sErrMsg);
	m_nTotalCount = vecRecord.size();
	if (m_nTotalCount % m_nPerPageCount == 0)
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount;
	}
	else
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount + 1;
	}
	if (m_nCurrentPage > m_nPageCount)
	{
		m_nCurrentPage = 1;
	}
	ui.labelPage->setText(A2T("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	if (m_nCurrentPage == 1)
	{
		ui.pushButtonUp->setEnabled(false);
	}
	else
	{
		ui.pushButtonUp->setEnabled(true);
	}
	if (m_nCurrentPage == m_nPageCount)
	{
		ui.pushButtonDown->setEnabled(false);
	}
	else
	{
		ui.pushButtonDown->setEnabled(true);
	}
	//计算只展示的内容
	int nStart = (m_nCurrentPage - 1) * m_nPerPageCount;//第一页0-99 第二页100-199 共101条
	int nEnd = (m_nCurrentPage - 1) * m_nPerPageCount + m_nPerPageCount;
	if (nEnd > m_nTotalCount)
	{
		nEnd = m_nTotalCount;
	}
	QVector<QVector<QString>>vecStr;
	vecStr.clear();
	for (int i = nStart; i < nEnd; i++)
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(i));//序号
		vecRow.push_back(vecRecord[i].value("RESULT_NAME").toString());//报告名
		vecRow.push_back(vecRecord[i].value("RESULT_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//报告生成时间
		vecRow.push_back(vecRecord[i].value("RESULT_PATH").toString());//报告路径
		vecRow.push_back(vecRecord[i].value("RESULT_PATH_URL").toString());//报告路径
		vecStr.push_back(vecRow);
	}

	ui.widget->InitData(3, vecStr);
}

void CVideoResultQuery::slotDelDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CVideoResultQuery::slotAddDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CVideoResultQuery::slotUp()
{
	if (m_nCurrentPage > 1)
	{
		m_nCurrentPage--;
	}
	slotPushButtonQuery();
}

void CVideoResultQuery::slotDown()
{
	if (m_nCurrentPage < m_nPageCount)
	{
		m_nCurrentPage++;
	}
	slotPushButtonQuery();
}

void CVideoResultQuery::slotOpenResult(QString sPath)
{
	if (sPath.size() == 0)
	{
		messageBox(0, A2T("没有查询到当前pdf报表!"));
		return;
	}

	//==========用默认网页打开该pdf=========
	// 构造文件URL
	QUrl pdfFileUrl(sPath);

	// 打开默认的Web浏览器
	if (!QDesktopServices::openUrl(pdfFileUrl))
	{
		messageBox(2, A2T("打开报表失败"));
		return;
	}
}
